package com.kpx.app.view.webview;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Message;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.kpx.app.view.MyWebView;
import com.kpx.app.utils.uiutils.ToastUtil;


/**
 * 监听webview弹出框类
 *
 * @author zhou wan
 * @Description TODO
 * @date 2015年4月7日
 * @Copyright: Copyright (c) 2015 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
 */
public class MyShopWebChromeClient extends WebChromeClient {

    private final Context context;
    public static int AlertType = -1;
    public static int TYPE_SUPER_MARKET_SHARE_PRODUCT = 4;//商品分享
    public static int TYPE_SUPER_MARKET_CHECK_IS_UNUSED = 3;//商品无效
    public static int TYPE_SUPER_MARKET_GET_BUY = 2;//商品购买
    public static int TYPE_SUPER_MARKET_GET_ADD_CART = 1;//商品添加

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
    }

    @Override
    public void onHideCustomView() {

    }

    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
    }

    /**
     * @param context
     * @version 1.0
     * @createTime 2015年4月27日, 下午7:55:54
     * @updateTime 2015年4月27日, 下午7:55:54
     * @createAuthor zhow wan
     * @updateAuthor
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public MyShopWebChromeClient(Context context, MyWebView myWebView) {
        this.context = context;
    }

    @Override
    public void onCloseWindow(WebView window) {
        super.onCloseWindow(window);
    }


    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        ShowMessage(view, message);
        result.confirm();
        return true;
    }

    @Override
    public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
        // ShowMessage(view, message);
        // result.confirm();
        return super.onJsBeforeUnload(view, url, message, result);
    }

    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        return true;
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        // pb.setProgress(newProgress);
        // if (newProgress == 100) {
        // pb.setVisibility(View.GONE);
        // } else if (newProgress < 100) {
        // pb.setVisibility(View.VISIBLE);
        // }
        super.onProgressChanged(view, newProgress);
    }

    @Override
    public void onReceivedIcon(WebView view, Bitmap icon) {
        super.onReceivedIcon(view, icon);
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
    }

    @Override
    public void onRequestFocus(WebView view) {
        super.onRequestFocus(view);
    }

    /**
     * 替换网页的弹窗
     *
     * @param view
     * @param message
     * @version 1.0
     * @createTime 2015年8月19日, 下午3:10:05
     * @updateTime 2015年8月19日, 下午3:10:05
     * @createAuthor yanzhongzhong
     * @updateAuthor yanzhongzhong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public void ShowMessage(final WebView view, String message) {
        ToastUtil.getInstance().showToast(context, message);
    }

}
